home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl -w
- # $Id: group.pl,v 1.3 1999/08/02 15:06:00 ray Exp $
- #
- # Adds a group to /etc/group, using gid as the lowest acceptable id.
- # Special IDs are :sys: (start at 40), and :user: (start at 100).
- #
- # Usage: group --create name gid [member member ...]
- #
- # Copyright (C) 1999 Caldera Systems, Inc.
-
- $file = "/etc/group";
- $minsysgid = 40;
- $minusrgid = 100;
-
- $command = shift;
-
- # For debugging
- if ($command =~ /-F(.*)/) {
- $file = $1;
- $command = shift;
- }
-
- if ($command =~ /(--)?create/ ) {
- $Is = $Iu = $i = 0;
- $name = shift || die( "Error: group-name missing\n");
- $gid = shift || die( "Error: group-id missing\n");
- $mem = join(',',@ARGV);
-
- open GIN,"<$file";
- #@L = <GIN>;
- while ( <GIN> ) {
- push( @L, $_);
- if ( /^([^:]+):[^:]*:([0-9]+):/ ) {
- $N{$2} = $1;
- $G{$1} = $2;
- $Is = $#L if ( $2 == $minsysgid );
- $Iu = $#L if ( $2 == $minusrgid );
- }
- }
- close( GIN);
-
- if ( exists( $G{$name}) ) {
- print STDERR "Warning: group '$name' exists (keeping '$G{$name}').\n"
- unless ( $G{$name} eq $gid );
- exit( 0);
- } elsif ( exists( $N{$gid}) ) {
- print STDERR "Error: group-id '$gid' already used by '$N{$gid}'.\n";
- exit( 1);
- } elsif ($gid eq ":sys:") {
- $gid = $minsysgid;
- } elsif ($gid eq ":user:") {
- $gid = $minusrgid;
- } elsif ( $gid !~ /^[1-9][0-9]*$/ ) {
- print STDERR "Error: Invalid group-id '$gid'.\n";
- exit( 1);
- }
- # Group not listed--here we go!
- $i = $Is if ( $gid >= $minsysgid );
- $i = $Iu if ( $gid >= $minusrgid );
- while ( exists( $N{$gid}) ) {
- $gid++;
- }
- if ( $gid > ((exists($G{nobody}))?$G{nobody}:65534) ) {
- print STDERR "Error: group-id overflow!\n";
- exit( 1);
- }
- for ( ; $i <= $#L; $i++) {
- last if ($L[$i] =~ /^\+/);
- next unless ($L[$i] =~ /^[^:]+:[^:]*:([0-9]+):/);
- last if ( $1 > $gid );
- }
- splice(@L,$i,0,"${name}::$gid:$mem\n");
- open GOUT, ">$file.new" || die;
- print GOUT @L;
- rename("$file.new", "$file") || die;
- 0;
- } elsif ($command =~ /(--)?revoke/ ) {
- print STDERR "Info: group revocation (currently?) not supported!\n";
- 0;
- } else {
- print STDERR "Error: Unknown command '$command'.\n";
- 1;
- }
-